<?php
header("Content-Type: text/html;charset=utf-8");
include('Includes/DB.inc.php');
include('Includes/AC.inc.php');

//获取用户权限
$uid = '1';
$dbh = db::connect();
$sql = "SELECT purview FROM p_ac_users WHERE uid=:uid LIMIT 1";
$sth = $dbh->prepare($sql);
$dbh = null;
$sth->bindParam(':uid',$uid);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
$purview = unserialize($result['purview']);


?>
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<title> 权限设置 </title>
	<style>
		h6 {font-size:16px;margin:10px;}
		li {margin:10px 0;}
		.msg {font-size:13px;margin:3px 10px;}
	
		#postPurview {
			float:left;

			margin-right:100px;
		}

		#getPurview {
			float:left;
		}

	</style>

	<script src="js/jquery.js"></script>
	<script>
	$(document).ready(function(){
		$("form").submit(function(e) {
			$.post(
				'_dev.php',
				$('form').serialize(),
				function(data,status) {
					$("#postPurview .msg").html(data);
					getPurview();
				}
			);
			return false;
		});


		//获取权限
		getPurview = function() {
			$.post(
				'_dev.php',
				{status:'getPurview'},
				function(data,status) {
					$("#getPurview pre").html(data);
				}
			);
		}
		$("#getPurview button").click(function(e) {
			getPurview();
			return false;
		});

	});
	</script>
</head>
<body>


<div id="postPurview">
	<form method="post">
		<?php foreach(ac::buildAppList() as $app => $appRes) { ?>

			<div style="margin:20px 0;">
				<h6><?=$appRes['name']?></h6>
				<div style="margin-left:30px;">
					<ul>
					<?php foreach($appRes['funs'] as $fun => $funRes) { ?>
						<li>
							<span><?=$funRes['name']?></span>
							<span>
								<?php foreach($funRes['acts'] as $act => $actName) {
									//选中已经拥有的权限
									if(isset($purview[$app][$fun])) {
										if(in_array($act,$purview[$app][$fun])) {
											$checked = ' checked';
										} else {$checked = '';}
									} else {$checked = '';}
									//输出
									echo '<input type="checkbox" name="purview['.$app.']['.$fun.'][]" value="'.$act.'"'.$checked.'>'.$actName;
								} ?>
							</span>
						</li>
					<?php } ?>
					</ul>
				</div>
			</div>

		<?php } ?>
		
		<input type='hidden' name='status' value="postPurview">
		<input type="submit" value="提交">
		<span class="msg"></span>
	</form>
</div>


<div id="getPurview">
	<button>读取</button>
	<pre><?php print_r($purview); ?></pre>
</div>



</body>
</html>